<?php
	$set_con = 0;
	$set_wrt = 1;
	
	if($set_con==1) {
		// open connection to database
		// mysqli_connect(host,username,password,dbname);
		$con = mysqli_connect("localhost","root","","cusres");

		// Check connection
		if (mysqli_connect_errno()) {
		  echo "Failed to connect to MySQL: " . mysqli_connect_error();
		}
		
		$result = mysqli_query($con,"SELECT * FROM Table1");

		//while($row = mysqli_fetch_array($result)) {
			//echo $row['FirstName'] . " " . $row['LastName'];
			//echo "<br>";
		//}
	}
	
	if($set_wrt==1) {

		// init
		$prefix = "KWETW_";
		$year = date("Y");
		$month = date("m");
		$date = date("d");
		$separator = "-";
		$hour = date("H");
		$minute = date("i");
		$counter_num = 1;
		$counter_pad = str_pad(($counter_num), 2, "0", STR_PAD_LEFT);
		$file_extension = ".EDI";

		$filename = $prefix.$year.$month.$date.$separator.$hour.$minute.$counter_pad.$file_extension;
		//$filename = "KWETW_20140425-100759.EDI";
		
		// increment counter check
		while (file_exists($filename)) {
			$counter_num += 1;
			$counter_pad = str_pad(($counter_num), 2, "0", STR_PAD_LEFT);
			$filename = $prefix.$year.$month.$date.$separator.$hour.$minute.$counter_pad.$file_extension;
		}
		
		// begin open or create file
		$myfile = fopen($filename, "w") or die("Unable to open file ".$filename." !");
		
		// begin writing text into file
		$txt = "";
		
		$txt .= "UNA:+.?'";
		$txt = "Ronald ~\n";
		fwrite($myfile, $txt);
		$txt = "Regan !\n";
		fwrite($myfile, $txt);
		
		// close file
		fclose($myfile);
		
		if(file_exists($filename)) { echo "file : ".$filename." has been created !"; }
	}

	if($set_con==1) {
		// close connection
		mysqli_close($con);
	}
?>
